﻿using System;
using System.IO;
using System.Windows.Forms;

namespace AutoMerge
{
    public class CopyFilesToDest
    {
        public static void copyTo(string destination, RichTextBox tb)
        {
            try
            {
                tb.AppendText("\r\n");
                tb.AppendText("coping files to " + destination + "--------------------------------------------------------------" + "\r\n");

                string destinationLoc = @destination;
                FileInfo machineFile = new FileInfo(@".\output\MachineManifest.config");
                FileInfo roleFile = new FileInfo(@".\output\RoleManifest.config");
                FileInfo actionFile = new FileInfo(@".\output\ActionManifest.config");

                FileInfo temp1 = new FileInfo(@Path.Combine(destinationLoc, machineFile.Name));
                FileInfo temp2 = new FileInfo(@Path.Combine(destinationLoc, roleFile.Name));
                FileInfo temp3 = new FileInfo(@Path.Combine(destinationLoc, actionFile.Name));

                //if the destination folder does not exist, create it
                if (!Directory.Exists(destinationLoc))
                {
                    tb.AppendText("creating folder: " + destinationLoc + "..." + "\r\n");
                    Directory.CreateDirectory(destinationLoc);
                }

                //if the file is existed, remove the readonly attribute, in order to delete it
                if (temp1.Exists)
                {
                    temp1.IsReadOnly = false;
                }
                if (temp2.Exists)
                {
                    temp2.IsReadOnly = false;
                }
                if (temp3.Exists)
                {
                    temp3.IsReadOnly = false;
                }

                //copy MachineManifest.config to destination
                tb.AppendText("copying MachineManifest.config to " + destinationLoc + "..." + "\r\n");
                machineFile.CopyTo(Path.Combine(destinationLoc, machineFile.Name), true);

                //copy RoleManifest.config to destination
                tb.AppendText("copying RoleManifest.config to " + destinationLoc + "..." + "\r\n");
                roleFile.CopyTo(Path.Combine(destinationLoc, roleFile.Name), true);

                //copy ActionManifest.config to destination
                tb.AppendText("copying ActionManifest.config to " + destinationLoc + "..." + "\r\n" + "\r\n");
                actionFile.CopyTo(Path.Combine(destinationLoc, actionFile.Name), true);

                //set the readonly to false, in order to delete the two files on local
                machineFile.IsReadOnly = false;
                roleFile.IsReadOnly = false;
                actionFile.IsReadOnly = false;

                //delete MachineManifest.configon local
                tb.AppendText("deleting file MachineManifest.config on local..." + "\r\n");
                machineFile.Delete();

                //delete RoleManifest.configon local
                tb.AppendText("deleting file RoleManifest.config on local..." + "\r\n");
                roleFile.Delete();

                //delete ActionManifest.configon local
                tb.AppendText("deleting file ActionManifest.config on local..." + "\r\n");
                actionFile.Delete();

                tb.AppendText("\r\n");
                tb.AppendText("Finished, the xmls are already at " + destinationLoc + "...");
            }

            catch (Exception ex)
            {
                tb.AppendText("Error occures when copying file to " + destination + "\r\n");
                tb.AppendText("Please make sure the path is correct and you have the permission to access the destination folder" + "\r\n");
                tb.AppendText(ex + "\r\n");
                throw new Exception("custom");
            }
        }
    }
}
